Page({
    data:{
        topHear:'0',
        ismale:getApp().globalData.UserData.sex==2?false:true,
        sex:getApp().globalData.UserData.sex==2?0:1,
        age:getApp().globalData.UserData.max_heart,
        heartRate:getApp().globalData.UserData.max_heart,
        showmodel:false,
    },
    onLoad(){
      this.setData({
        topHear: '0',
        ismale: getApp().globalData.UserData.sex == 2 ? false : true,
        sex: getApp().globalData.UserData.sex == 2 ? 0 : 1,
        age: getApp().globalData.UserData.max_heart,
        heartRate: getApp().globalData.UserData.max_heart,
        showmodel: false
      })
    },
    tapOuter:function(){
        this.setData({
            showmodel:false
        })
    },
    tapInner:function(e){
        
    },
    showTap:function(){
        this.setData({
            showmodel:true
        })
    },
    radioChange:function(e){
        var value = e.detail.value;
        this.setData({
            sex:value==1?1:0,
            ismale:value==1?true:false
        })
        console.log(this.data.sex);
    },
    confirmAge:function(){
        if(isNaN(this.data.age)){
            wx.showModal({
                title: '提示',
                content: '年龄格式错误',
                showCancel:false
            })
            return;
        }

        if(this.data.age>220){
            wx.showModal({
                title: '温馨提醒',
                content: '>_<',
                showCancel:false
            })
            return;
        }
        if(this.data.sex==1){
            this.setData({
                heartRate:220-this.data.age,
                showmodel:false
            })
        }else{
            this.setData({
                heartRate:226-this.data.age,
                showmodel:false
            })
        }
    },changeAge:function(e){
        this.setData({
            age:e.detail.value
        })
    },changeHeart:function(e){
        this.setData({
            heartRate:e.detail.value
        })
    },
    confirmHeart:function(){
        let self = this;
        if(isNaN(this.data.heartRate)||this.data.heartRate==0){
             wx.showModal({
                title: '提示',
                content: '为确保数据的准确，请正确填写您的心率',
                showCancel:false
            })
            return;
        }
        getApp().globalData.topHeart = this.data.heartRate;
        getApp().globalData.UserData.max_heart = self.data.heartRate;
        getApp().request({
          url: 'set_max_heart',
          data: {
              id:getApp().globalData.UserData.id,
              max_heart:self.data.heartRate
          },
          success: function(res){
            if(res.data.flag){
              getApp().globalData.UserData.max_heart = self.data.heartRate
              self.setData({
                heartRate:self.data.heartRate
              })
                wx.navigateBack({
                  delta: 1
                })
            }else{
                 wx.showModal({
                    title: '提示',
                    content: res.data.msg,
                    showCancel:false
                })
            }
          },
          fail: function(res) {
            wx.showModal({
                content: '请求失败',
                showCancel:false,
                success: function(res) {
                }
            })
          }
        })
        
    }

})